package com.czk.springcloud.controller;

import com.czk.springcloud.service.OrderHystrixOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/12/18 16:29
 */
@RestController
@RequestMapping("/consumer")
@Slf4j
@DefaultProperties(defaultFallback = "globalFallBackMethod")
public class OrderHystrixController {
    @Autowired
    private OrderHystrixOrderService orderHystrixOrderService;

    /**
     * 成功方法
     *
     * @param id
     * @return
     */
    @GetMapping("/hystrix/ok/{id}")
    public String payment_ok(@PathVariable("id") Integer id) {
        return orderHystrixOrderService.payment_ok(id);
    }

    /**
     * 失败方法
     *
     * @param id
     * @return
     */
    @GetMapping("/hystrix/fail/{id}")
    // 如果自己指定了服务降级的方法，则走自己指定的服务降级方法
  /*  @HystrixCommand(fallbackMethod = "paymentInfo_FailHandler", commandProperties = {
            // "execution.isolation.thread.timeoutInMileSeconds这个线程超时2秒钟事则会调用兜底方法
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")})*/
    /**
     * 测试全局服务降级方法
     */
    @HystrixCommand
    public String payment_fail(@PathVariable("id") Integer id) {
        return orderHystrixOrderService.payment_fail(id);
    }
    /**
     * payment_fail的兜底方法
     *
     * @param id
     * @return
     */
    public String paymentInfo_FailHandler(Integer id) {
        return "我是消费，由于服务端没响应，请稍后再试" + "o(╥﹏╥)o";
    }

    /**
     * 全局服务降级方法
     * @return
     */
    public String globalFallBackMethod(){
        return "这是全局服务降级方法";
    }
}
